package com.cheer.ba.view.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改个人信息
 *
 * @author HQ
 *         created at 2017/4/12 0012 16:24
 */
public class ModifyPersonInfoAty extends BaseCommAty {

    @BindView(R.id.id_person_name_ll)
    LinearLayout mModifyNameLL;
    //修改昵称
    @BindView(R.id.id_input_user_name_edit)
    EditText mEditModifyName;
    private String mSaveUserName;
    @BindView(R.id.id_clear_name_img)
    ImageView mImgClearName;

    //修改性别
    @BindView(R.id.id_modify_sex_ll)
    LinearLayout mModifySexLL;
    @BindView(R.id.id_check_men)
    CheckBox mCheckMen;
    @BindView(R.id.id_check_women)
    CheckBox mCheckWomen;

    private String params;
    private int type;


    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_modify_person_info, null, false));
    }

    @Override
    public void initView() {
        params = getIntent().getStringExtra(IntentKey.Personal.KEY_MODIFY_PARAMS);
        type = getIntent().getIntExtra(IntentKey.General.KEY_TYPE, Constants.DEFAULTVALUE);
        setTitleText(Protect.checkIsNull(params));
        setTitleRight(getString(R.string.sure_btn), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == IntentCode.Setting.PERSON_MODIFY_NAME) {
                    if (StringUtils.isEmpty(mSaveUserName)) {
                        SVProgressHUD.showInfoWithStatus(aty, getString(R.string.personal_modify_name));
                        return;
                    }
                    Intent name = new Intent();
                    name.putExtra(IntentKey.Personal.KEY_MODIFY_PARAMS, mSaveUserName);
                    setResult(RESULT_OK, name);
                    finish();
                } else if (type == IntentCode.Setting.PERSON_MODIFY_SEX) {
                    Intent sex = new Intent();
                    if (mCheckMen.isChecked()) {
                        sex.putExtra(IntentKey.Personal.KEY_MODIFY_PARAMS, "1");
                    } else {
                        sex.putExtra(IntentKey.Personal.KEY_MODIFY_PARAMS, "0");
                    }
                    setResult(RESULT_OK, sex);
                    finish();
                }
            }
        });
    }

    @Override
    public void initData() {
        if (type == IntentCode.Setting.PERSON_MODIFY_NAME) {
            mModifyNameLL.setVisibility(View.VISIBLE);
            mEditModifyName.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    mSaveUserName = s + "";
                    if (StringUtils.isEmpty(mSaveUserName)) {
                        mImgClearName.setVisibility(View.GONE);
                    } else {
                        mImgClearName.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
            mEditModifyName.setHint(Session.getUserName());
        } else if (type == IntentCode.Setting.PERSON_MODIFY_SEX) {
            mModifySexLL.setVisibility(View.VISIBLE);
            if (TextUtils.equals(Session.getUser_sex(), getString(R.string.personal_sex_men))) {
                mCheckMen.setBackgroundResource(R.drawable.ic_yes);
                mCheckWomen.setBackgroundResource(R.color.white);
                mCheckMen.setChecked(true);
                mCheckWomen.setChecked(false);
            } else {
                mCheckMen.setBackgroundResource(R.color.white);
                mCheckWomen.setBackgroundResource(R.drawable.ic_yes);
                mCheckMen.setChecked(false);
                mCheckWomen.setChecked(true);
            }
        }
    }

    @OnClick({R.id.id_clear_name_img, R.id.id_check_men_ll, R.id.id_check_men, R.id.id_check_women_ll, R.id.id_check_women})
    public void onClick(View view) {
        switch (view.getId()) {
            //清除昵称
            case R.id.id_clear_name_img:
                mEditModifyName.setText("");
                mEditModifyName.setHint("");
                break;
            case R.id.id_check_men_ll:
            case R.id.id_check_men:
                mCheckMen.setBackgroundResource(R.drawable.ic_yes);
                mCheckWomen.setBackgroundResource(R.color.white);
                mCheckMen.setChecked(true);
                mCheckWomen.setChecked(false);
                break;
            case R.id.id_check_women_ll:
            case R.id.id_check_women:
                mCheckMen.setBackgroundResource(R.color.white);
                mCheckWomen.setBackgroundResource(R.drawable.ic_yes);
                mCheckMen.setChecked(false);
                mCheckWomen.setChecked(true);
                break;
        }
    }

}
